/*
 *  This file is part of Bracket Properties
 *  Copyright 2011-2013 David R. Smith
 *  All Rights Reserved
 *
 */
package asia.redact.bracket.properties.serializer;

import asia.redact.bracket.util.SerialIDGenerator;

/**
 * Implement the Factory pattern to get tokens of various kinds. The tokens represent objects in the graph, and
 * are generated as we walk the graph. 
 * 
 * @author Dave
 *
 */
public class TokenFactory {
	
	SerialIDGenerator idGen;
	
	public static final TokenFactory INSTANCE = new TokenFactory();

	private TokenFactory() {
		super();
		idGen = SerialIDGenerator.INSTANCE;
	}
	
	public Token createObjectToken(String key, Object obj) {
		return new Token(idGen.increment(),TokenType.O,key,obj);
	}
	
	public Token createEnumToken(Object obj) {
		return new Token(idGen.increment(),TokenType.E,"name",obj);
	}
	
	public Token createListToken(String key, Object obj) {
		return new Token(idGen.increment(),TokenType.LI,key,obj);
	}
	
	public Token createPrimitiveToken(String key, Object obj) {
		return new Token(idGen.increment(),TokenType.P,key,obj);
	}
	
	public Token createMapToken(String key, Object obj) {
		return new Token(idGen.increment(),TokenType.M,key,obj);
	}
	
	public Token nullToken() {
		return Token.NULL_WRAPPER;
	}
	
	public Token createToken(TokenType type, String key, Object obj){
		if(type.isRenderImmediate()){
			// bit of a hack, here
			return new Token(-1,type,key,obj);
		}else{
			return new Token(idGen.increment(),type,key,obj);
		}
	}
	
	public Token createToken(long id, TokenType type, String key, Object obj){
			return new Token(id,type,key,obj);
	}
	
	public Token createTmpToken(Object obj){
		TokenType type = MethodGetter.testType(obj);
		if(type.isRenderImmediate()){
			return new Token(-1,type,"",obj);
		}else{
			return new Token(idGen.increment(),type,"",obj);
		}
	}
	
	public void resetSerialID() {
		idGen.initialize();
	}
	
	public long currentSerialID() {
		return idGen.currentValue();
	}

}
